package com.zhouxiaoge.socket.multithreading;

import java.io.*;
import java.net.Socket;
import java.util.Random;

/**
 * @author zmy
 */
public class Clinet {
    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {

            try {
                Socket socket = new Socket("localhost", 8888);
                OutputStream outputStream = socket.getOutputStream();
                PrintWriter printWriter = new PrintWriter(outputStream);
                printWriter.write("用户名：zhouxiaoge, 密码：" + new Random().nextInt(10000));
                printWriter.flush();
                socket.shutdownOutput();
                InputStream inputStream = socket.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String info;
                while ((info = bufferedReader.readLine()) != null) {
                    System.out.println("我是客户端，服务器说: " + info);
                }
                bufferedReader.close();
                inputStream.close();
                printWriter.close();
                outputStream.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
